package se.webbzon.boltzmann.game.object.dialogue.word;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3d;

import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.oschi01.worldobject.appearance.Animation3D;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;

public class TremblingDialogueWord extends DividedDialogueWord {
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new trembling dialogue word with the default color. **/
	public TremblingDialogueWord(DialogueLayout layout, String word) {
		super(layout,word);
		getAppearance().setAnimation(new TrembleAnimation());
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class TrembleAnimation implements Animation3D {

		@Override public void onStepEnd(Appearance3D appearance) {
			final int length = getWord().length();
			final DialogueLayout layout = getLayout();
			final double width = layout.getCharacterWidth();
			final double height = layout.getCharacterHeight();
			
			final Transform3D transform = new Transform3D();
			final Vector3d translation = new Vector3d();
			
			for (int i = 0; i < length; i++) {
				final DividedDialogueCharacter character = getCharacter(i);
				translation.set(width * (i + 0.25 * (Math.random() - 0.5)), 
								0.25 * height * (Math.random() - 0.5), 
								0);
				character.getTransform(transform);
				transform.setTranslation(translation);
				character.setTransform(transform);
			}
		}
	}

}
